//
//  DetailRegViewController.m
//  TT
//
//  Created by lanou3g on 16/3/11.
//  Copyright © 2016年 Super磊. All rights reserved.
//

#import "DetailRegViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "TTViewController.h"
#import "DrawerViewController.h"
#import "LeftMenuViewController.h"
#import "MessageViewController.h"
#import "AppDelegate.h"
#import <EaseMob.h>

@interface DetailRegViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *uesrAge;
@property (weak, nonatomic) IBOutlet UITextField *birthday;
@property (weak, nonatomic) IBOutlet UIImageView *imV;
@property (weak, nonatomic) IBOutlet UISwitch *Men;
@property (weak, nonatomic) IBOutlet UISwitch *women;
@property (weak, nonatomic) IBOutlet UITextField *userHobby;
//协议
@property (weak, nonatomic) IBOutlet UIButton *protocol;

@end

@implementation DetailRegViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   [AVOSCloud setApplicationId:@"pLGUE2TepdIMn4pfjc9UOogY-gzGzoHsz" clientKey:@"Issl3J2BacCnbCCSk99vqs3K"];
    self.imV.image =[UIImage imageNamed:@"7.jpg"];
    self.Men.on =!self.women.on;
    UIBarButtonItem *item =[[UIBarButtonItem alloc]initWithTitle:@"注册新用户" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightAction)];
    self.navigationItem.rightBarButtonItem =item;
    
    
    
}
-(void)rightAction {
    
    //    注册
    [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.mobilePhoneNumber password:[NSString stringWithFormat:@"%@%@",self.mobilePhoneNumber,self.mobilePhoneNumber] withCompletion:^(NSString *username, NSString *password, EMError *error) {
        if (!error) {
            NSLog(@"注册成功");
        }
    } onQueue:nil];

    AVObject *detailReg = [AVObject objectWithClassName:@"DetailReg"];
    [detailReg setObject:self.birthday.text forKey:@"userBirthday"];
    [detailReg setObject:self.mobilePhoneNumber forKey:@"mobilePhoneNumber"];
    if (self.Men.on) {
        [detailReg setObject:@"男" forKey:@"userGender"];
    }else {
        
        [detailReg setObject:@"女" forKey:@"userGender"];
    }
   
    [detailReg setObject:self.uesrAge.text forKey:@"userAge"];
    [detailReg setObject:self.userHobby.text forKey:@"userHobby"];
     //异步传输
//    [detailReg saveInBackground];
    //上传照片
    UIImage *image =self.imV.image;
    //将图片对象转换为NSData数据类型
    NSData *imageData = UIImagePNGRepresentation(image);
    //创建AVFile对象保存图片
    AVFile *imageFile = [AVFile fileWithName:@"image.png" data:imageData];
    
    [detailReg setObject:imageFile forKey:@"userImage"];
    //异步保存数据
    [detailReg saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        }
        else {
            // 设置应用默认的配置
            [self setUserDefault];
            NSLog(@"图片上传成功");
        }
    }];

    
//    TTViewController *tt =[TTViewController new];
//    [self.navigationController pushViewController:tt animated:YES];
    AppDelegate *app =[UIApplication sharedApplication].delegate;
    app.draw =[[DrawerViewController alloc]init];
    TTViewController *ttVC =[[TTViewController alloc]init];
    UINavigationController *naV =[[UINavigationController alloc]initWithRootViewController:ttVC];
    ttVC.mobilePhoneNumber = self.mobilePhoneNumber;
    [app.draw setRootViewController:naV];
    //设置侧滑栏
    LeftMenuViewController *left =[LeftMenuViewController new];
    MessageViewController *right =[MessageViewController new];
    [app.draw setLeftVC:left];
    [app.draw setRightVC:right];
    [self presentViewController:app.draw animated:YES completion:nil];

//    [self.navigationController pushViewController:app.draw animated:YES];
}

- (IBAction)upLoadFile:(id)sender {
    //调用相册
    UIImagePickerController *picCon = [[UIImagePickerController alloc]init];
    picCon.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picCon.delegate = self;
    picCon.allowsEditing = YES;
    [self presentViewController:picCon animated:YES completion:nil];

    
    
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    self.imV.image=[info valueForKey:@"UIImagePickerControllerEditedImage"];
    //模态窗口返回
    [picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
//协议
- (IBAction)protocol:(id)sender {
}

- (void)setUserDefault {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setValue:self.mobilePhoneNumber forKey:@"mobilePhoneNumber"];
    [user setValue:@18 forKey:@"firstAge"];
    [user setValue:@28 forKey:@"secondAge"];
    [user setValue:@10000 forKey:@"distance"];
    [user setValue:@"男" forKey:@"sex"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
